﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;

namespace Jumpman.src
{
    public class MenuSelector : GameElement
    {
        const int MENU_POS_Y = 340;
        const int MENU_POS_X = 335;
        const int MENU_STEP = 5;
        const int MENU_STEP_DISTANCE = MENU_GAP / MENU_STEP;
        const int MENU_GAP = 58;
        const int MAX_INDEX = 1;
        const int MIN_INDEX = 0;

        private int selectedIndex = 0;
        private int menuStepCounter = 0;
        private int movingDir;

       private IAction gtl = new GoToLevel();


        public MenuSelector()
        {
            IsStatic = true;
            ElementSprite = Sprites.CreateSprite(Resources.LoadBitmap("menumush.png"));
            ElementSprite.X = MENU_POS_X;
            ElementSprite.Y = MENU_POS_Y;
            movingDir = 0;
        }



        void moveSelector(int dir)
        {

            ElementSprite.Y += (dir * MENU_STEP_DISTANCE);

        }

        public override void Update(GameController gc)
        {
            if ((movingDir == 1) || Input.KeyDown(KeyCode.vk_DOWN) && selectedIndex < MAX_INDEX)
            {
                if (menuStepCounter < MENU_STEP)
                {
                    moveSelector(1);
                    movingDir = 1;
                    menuStepCounter++;

                }


                else if (menuStepCounter >= MENU_STEP)
                {
                    movingDir = 0;
                    selectedIndex++;
                    menuStepCounter = 0;

                }
            }

            else if ((movingDir == -1) || Input.KeyDown(KeyCode.vk_UP) && selectedIndex > MIN_INDEX)
            {
                if (menuStepCounter < MENU_STEP)
                {
                    moveSelector(-1);
                    movingDir = -1;
                    menuStepCounter++;
                }
                else if (menuStepCounter >= MENU_STEP)
                {
                    selectedIndex--;
                    movingDir = 0;
                    menuStepCounter = 0;
                }
            }

            else if (Input.KeyTyped(KeyCode.vk_RETURN))
            {
                switch (selectedIndex)
                {
                    case 0:
                        gtl.DoAction(gc);

                        break;
                    case 1:
                        //gotolevel;
                        break;
                }
            }

        }


        public override void Draw()
        {
            ElementSprite.Draw();
        }



    }
}
